package cn.jingyuan.owl.utils.core.convert.impl;

import cn.jingyuan.owl.utils.core.ObjectUtils;
import cn.jingyuan.owl.utils.core.convert.AbstractConverter;
import cn.jingyuan.owl.utils.core.map.MapUtils;

import java.util.Map;

/**
 * {@link StackTraceElement} 转换器<br>
 * 只支持 Map 方式转换
 */
public class StackTraceElementConverter extends AbstractConverter<StackTraceElement> {

    @Override
    protected StackTraceElement convertInternal(Object value) {
        if (value instanceof Map) {
            final Map<?, ?> map = (Map<?, ?>) value;

            final String declaringClass = MapUtils.getString(map, "className");
            final String methodName = MapUtils.getString(map, "methodName");
            final String fileName = MapUtils.getString(map, "fileName");
            final Integer lineNumber = MapUtils.getInt(map, "lineNumber");

            return new StackTraceElement(declaringClass, methodName, fileName, ObjectUtils.defaultIfNull(lineNumber, 0));
        }
        return null;
    }

}
